﻿/**
 * Copyright © TnT-VNGroup 2011
 * PROJECT:   SmartCollector
 * $HeadURL: http://smartcollector.googlecode.com/svn/branches/Srcs/SmartCollector.GUI/Helpers/DeSerializeHelper.cs $
 * $Author: chithanh12@gmail.com $
 * $Date: 2011-04-20 18:46:52 +0000 (Wed, 20 Apr 2011) $
 * $Rev: 94 $
 **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using SmartCollector.Domain;
using System.IO;
using SmartCollector.Domain.Entity;

namespace SmartCollector.GUI.Helpers
{
    public class DeSerializeHelper
    {
        /// <summary>
        /// Deserializes the specified item.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">The item.</param>
        /// <param name="fileName">Name of the file.</param>
        public static void Serialize<T>(T item, string fileName)
        {
            XmlWriter writer = null;
            try
            {
                writer = XmlWriter.Create(fileName, new XmlWriterSettings()
                {
                    Indent = true,
                    Encoding = Encoding.Unicode
                });
                XmlSerializer ser = new XmlSerializer(typeof(T));
                ser.Serialize(writer, item);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }

        /// <summary>
        /// Des the serialize.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">The item.</param>
        /// <param name="fileName">Name of the file.</param>
        public static void DeSerialize<T>(T item, string fileName)
        {
            XmlWriter writer = null;
            try
            {
                writer = XmlWriter.Create(fileName, new XmlWriterSettings()
                {
                    Indent = true,
                    Encoding = Encoding.Unicode
                });
                XmlSerializer ser = new XmlSerializer(typeof(T));
                ser.Serialize(writer, item);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }

        /// <summary>
        /// Saves to file.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="item">The item.</param>
        public static void SaveNewItemToFile(string fileName, tbl_Item item)
        {
            StringBuilder content = new StringBuilder();
            content.Append(item.Title);
            content.AppendLine();
            content.Append(item.Description);
            content.AppendLine();
            content.Append(item.Content);
            using (StreamWriter writer = new StreamWriter(fileName))
            {
                writer.Write(content.ToString());
            }
        }
    }
}
